﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Test_Drag_N_drop
{
    public class ConstructZone
    {
        private GraphicsDevice graphic;
        private Rectangle myBox;

        public Rectangle Box
        {
            get { return myBox; }
            set { myBox = value; }
        }

        private static ConstructZone instance;
        private static List<ConstructBlock> myList;
        private BlockDnD newBlock;
        private ContentManager content;
        private int currID = 1;
        private bool needToClear = false;
        private int blockToClear = 0;

        private ConstructZone()
        {

        }

        public static ConstructZone getInstance ()
        {
            if (instance == null)
            {
                instance = new ConstructZone();
            }
            return instance;
        }

        public void Initialize (ContentManager content, GraphicsDevice graphic, int sW, int sH)
        {
            this.content = content;
            this.graphic = graphic;
            myList = new List<ConstructBlock>();
            myList.Add( new BlockMain(0));
            myBox = new Rectangle((int)sW / 3, 0, (int)sW / 3, sH);
            myList[0].Initialize(graphic, myBox.X + myBox.Width / 2, 80, TypeOfBlock.MAIN);
        }

        public void LoadContent()
        {
            myList[0].LoadContent(content);
        }

        public void Update(GameTime gameTime, MouseState ms, MouseState pms)
        {
            foreach (ConstructBlock block in myList)
            {   
                block.Update(gameTime, ms, pms);
            }

            if (newBlock != null)
            {
                newBlock.Update(gameTime, ms, pms);
            }

            {
                if (needToClear)
                {
                    clearBlock(blockToClear);
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {

            foreach (ConstructBlock block in myList)
            {
                block.Draw(spriteBatch, gameTime, graphic);
            }
        }

        public void DrawNewBlock(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (newBlock != null)
            {
                newBlock.Draw(spriteBatch, gameTime, graphic);
            }
        }

        public List<ConstructBlock> getList()
        {
            return myList;
        }

        public void addBlock (BlockDnD block)
        {
            block.ID = currID;
            myList.Add(block);
            currID++;
            newBlock = null;
        }

        public void createNewBlock(TypeOfBlock type, int mX, int mY)
        {
            newBlock = new BlockDnD(-1);
            newBlock.Initialize(graphic, mX, mY, type);
            newBlock.LoadContent(content);

        }

        public void clearBlock(int id)
        {
            
            if (id == -1)
            {
                newBlock = null;
                needToClear = false;
            }
            else
            {   

                for ( int i = 0; i < myList.Count; i++)
                {
                    if (myList[i].Son != null && myList[i].Son.ID == id)
                    {
                        myList[i].Son = null;
                        myList[i].HaveSon = false;
                    }
                }
                for (int i = 0; i < myList.Count; i++)
                {
                    if (myList[i].ID == id)
                    {   
                        myList.RemoveAt(i);
                        needToClear = false;
                    }
                }
            }

            
        }

        public void shouldClearBlock(int id)
        {
            needToClear = true;
            blockToClear = id;
        }

        public void clearList()
        {
            myList.RemoveRange(1, myList.Count - 1);
            myList[0].Son = null;
            myList[0].HaveSon = false;

        }

    }
}
